package com.lkc.controller;

import com.lkc.bean.Categories;
import com.lkc.service.CategoriesService;
import com.lkc.util.Page;
import com.lkc.util.Result;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@CrossOrigin
@RequestMapping("categories")
public class CategoriesController {

    @Autowired
    private CategoriesService categoriesService;

    // 新增分类
    @PostMapping
    public Result add(@RequestBody Categories categories){
        Categories obj = categoriesService.add(categories);
        return Result.success(obj);
    }

    // 编辑分类
    @PutMapping
    public Result edit(@RequestBody Categories categories){
        Categories obj = categoriesService.edit(categories);
        return Result.success(obj);
    }

    // 删除分类
    @DeleteMapping("{id}")
    public Result del(@PathVariable("id") Integer id){
        int obj = categoriesService.del(id);
        return Result.success(obj);
    }

    // 查询单个分类
    @GetMapping("{id}")
    public Result one(@PathVariable("id") Integer id){
        Categories obj = categoriesService.one(id);
        return Result.success(obj);
    }

    // 查询所有分类
    @GetMapping("all")
    public Result all(Categories categories){
        List<Categories> obj = categoriesService.all(categories);
        return Result.success(obj);
    }

    // 分页查询
    @GetMapping("page")
    public Result page(Categories categories, Page page){
        categoriesService.page(page, categories);
        return Result.success(page);
    }
    // 在 CategoriesController.java 中添加以下方法
//    @GetMapping("list")
//    public Result list() {
//        Categories categories = new Categories();
//        categories.setIsDeleted(0); // 只查询未删除的分类
//        List<Categories> list = categoriesService.all(categories);
//        return Result.success(list);
//    }

}
